package com.steven.ourcontest.contest.view

import android.os.Bundle
import androidx.activity.viewModels
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.listener.OnTimeSelectListener
import com.steven.ourcontest.R
import com.steven.ourcontest.base.BaseActivity
import com.steven.ourcontest.contest.viewmodel.AddContestViewModel
import com.steven.ourcontest.utils.DateUtil
import com.steven.ourcontest.utils.toast
import kotlinx.android.synthetic.main.activity_add_match.*


class AddMatchActivity : BaseActivity() {

    private val addContestViewModel: AddContestViewModel by viewModels()
    private var signUpStartTime: Long? = null
    private var signUpEndTime: Long? = null
    private var matchStartTime: Long? = null
    private var matchEndTime: Long? = null
    override fun getStatusBarColor(): Int {
        return R.color.white
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun getViewId(): Int? {
        return R.layout.activity_add_match
    }

    override fun initView() {
        tv_signUpStartTime?.setOnClickListener {
            val pvTime = TimePickerBuilder(this, OnTimeSelectListener { date, v ->
                signUpStartTime = date.time
                tv_signUpStartTime?.text = DateUtil.timeStamp2Date(signUpStartTime, "yyyy-MM-dd")
            }).build()
            pvTime.show()
        }
        tv_signUpEndTime?.setOnClickListener {
            val pvTime = TimePickerBuilder(this, OnTimeSelectListener { date, v ->
                signUpEndTime = date.time
                tv_signUpEndTime?.text = DateUtil.timeStamp2Date(signUpEndTime, "yyyy-MM-dd")
            }).build()
            pvTime.show()
        }
        tv_matchStartTime?.setOnClickListener {
            val pvTime = TimePickerBuilder(this, OnTimeSelectListener { date, v ->
                matchStartTime = date.time
                tv_matchStartTime?.text = DateUtil.timeStamp2Date(matchStartTime, "yyyy-MM-dd")
            }).build()
            pvTime.show()
        }
        tv_matchEndTime?.setOnClickListener {
            val pvTime = TimePickerBuilder(this, OnTimeSelectListener { date, v ->
                matchEndTime = date.time
                tv_matchEndTime?.text = DateUtil.timeStamp2Date(matchEndTime, "yyyy-MM-dd")
            }).build()
            pvTime.show()
        }
        tv_confirm?.setOnClickListener {
            if (et_match?.text?.isNotEmpty() == false) {
                toast("请输入竞赛名称！")
                return@setOnClickListener
            }
            if (et_belong?.text?.isNotEmpty() == false) {
                toast("请输入主办方！")
                return@setOnClickListener
            }
            if (et_rank?.text?.isNotEmpty() == false) {
                toast("请输入级别！")
                return@setOnClickListener
            }
            if (signUpStartTime == null) {
                toast("请选择报名开始时间！")
                return@setOnClickListener
            }
            if (signUpEndTime == null) {
                toast("请选择报名结束时间！")
                return@setOnClickListener
            }
            if (matchStartTime == null) {
                toast("请选择比赛开始时间！")
                return@setOnClickListener
            }
            if (matchEndTime == null) {
                toast("请选择比赛结束时间！")
                return@setOnClickListener
            }
            addContestViewModel.addMatch(
                et_match?.text.toString(),
                et_belong?.text.toString(),
                et_rank?.text.toString(),
                signUpStartTime,
                signUpEndTime,
                matchStartTime,
                matchEndTime,
                et_url?.text?.toString(),
                success = {
                    toast("创建成功！")
                    finish()
                },
                error = {
                    toast(it)
                }
            )
        }
    }
}